//
//  RadarDish.m
//  LearningCocos
//
//  Created by abyssos on 2014. 2. 26..
//  Copyright (c) 2014년 ainhan. All rights reserved.
//

#import "RadarDish.h"

@implementation RadarDish

@synthesize tiltingAnim;
@synthesize transmittingAnim;
@synthesize takingAHitAnim;
@synthesize blowingUpAnim;

- (void)dealloc {
    [tiltingAnim release];
    [transmittingAnim release];
    [takingAHitAnim release];
    [blowingUpAnim release];
    
    [super dealloc];
}

- (void)changeState:(CharacterStates)newState {
    [self stopAllActions];
    id action = nil;
    self.characterState = newState;
    
    switch(newState) {
        case kStateSpawning:
            CCLOG(@"RadarDish -> Starting the Spawning Animation");
            tiltingAnim.restoreOriginalFrame = NO;
            action = [CCAnimate actionWithAnimation:tiltingAnim];
            break;
        case kStateIdle:
            CCLOG(@"RadarDish -> Changing State to Idle %@", transmittingAnim);
            transmittingAnim.restoreOriginalFrame = NO;
            action = [CCAnimate actionWithAnimation:transmittingAnim];
            break;
        case kStateTakingDamage:
            CCLOG(@"RadarDish -> Changing State to TakingDamage");
            characterHealth = characterHealth - [vikingCharacter getWeaponDamage];
            if(characterHealth <= 0.0f) {
                [self changeState:kStateDead];
            } else {
                takingAHitAnim.restoreOriginalFrame = NO;
                action = [CCAnimate actionWithAnimation:takingAHitAnim];
            }
            break;
        case kStateDead:
            CCLOG(@"RadarDish -> Changing State to Dead");
            blowingUpAnim.restoreOriginalFrame = NO;
            action = [CCAnimate actionWithAnimation:blowingUpAnim];
            break;
        default:
            CCLOG(@"Unhandled state %d in RadarDish", newState);
            break;
    }
    
    if(action != nil) {
        [self runAction:action];
    }
    
}

- (void)updateStateWithDeltaTime:(ccTime)deltaTime andListOfGameObjects:(CCArray *)listOfGameObjects {
    if(characterState == kStateDead)
        return;
    
    vikingCharacter = (GameCharacter *)[[self parent] getChildByTag:kVikingSpriteTagValue];
    
    CGRect vikingBoundingBox = [vikingCharacter adjustedBoundingBox];
    CharacterStates vikingState = [vikingCharacter characterState];
    
    // Calcurate if the Viking is attacking and nearby
    if((vikingState == kStateAttacking) && (CGRectIntersectsRect([self adjustedBoundingBox], vikingBoundingBox))) {
        if(characterState != kStateTakingDamage) {
            // If RadarDish is NOT alreadytaking Damage
            [self changeState:kStateTakingDamage];
            return;
        }
    }
    
    if(([self numberOfRunningActions] == 0) && (characterState != kStateDead)) {
        CCLOG(@"Going to Idle");
        [self changeState:kStateIdle];
        return;
    }
}

- (void)initAnimations {

    CCLOG(@"## [RadarDish] initAnimations...");
          
    [self setTiltingAnim:[self loadPlistForAnimationWithName:@"tiltingAnim" andClassName:NSStringFromClass([self class])]];
    [self setTransmittingAnim:[self loadPlistForAnimationWithName:@"transmittingAnim" andClassName:NSStringFromClass([self class])]];
    [self setTakingAHitAnim:[self loadPlistForAnimationWithName:@"takingAHitAnim" andClassName:NSStringFromClass([self class])]];
    [self setBlowingUpAnim:[self loadPlistForAnimationWithName:@"blowingUpAnim" andClassName:NSStringFromClass([self class])]];
}


- (id)initWithSpriteFrameName:(NSString*)spriteFrameName {
    CCLOG(@"## [RadarDish] initWithSpriteFrameName : %@", spriteFrameName);
    
    if((self = [super initWithSpriteFrameName:spriteFrameName])) {
        
        [self initAnimations];
        characterHealth = 100.0f;
        gameObjectType = kEnemyTypeRadarDish;
        [self changeState:kStateSpawning];
    }
    
    return self;
}

@end





































